package com.infra.micro.id.model.enums;

import com.infra.common.enums.BaseEnum;
import com.infra.common.resp.KeyValue;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public enum IdSnowflakeHolderTypeEnum implements BaseEnum {
    LOCAL(1, "本地"),
    MYSQL(2, "数据库"),
    ZOOKEEPER(3, "ZOOKEEPER"),
    RECYCLABLE_ZOOKEEPER(4, "可回收的ZOOKEEPER");
    /**
     * 状态码
     */
    private int code;

    /**
     * 状态码描述
     */
    private String message;

    IdSnowflakeHolderTypeEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 通过码获取枚举项
     *
     * @param code
     * @return
     */
    public static IdSnowflakeHolderTypeEnum getByCode(int code) {
        return Arrays.stream(IdSnowflakeHolderTypeEnum.values())
                .filter(item -> item.getCode() == code)
                .findFirst()
                .orElse(IdSnowflakeHolderTypeEnum.MYSQL);
    }

    /**
     * 枚举项值转为List
     * e.g. ChecksumEnum.toList();
     *
     * @return
     */
    public static List<KeyValue> toList() {
        return Arrays.stream(IdSnowflakeHolderTypeEnum.values())
                .map(item -> new KeyValue(String.valueOf(item.getCode()), item.getMessage()))
                .collect(Collectors.toList());
    }

}